package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.SysUser;
import com.example.server.web.domain.Teacher;
import com.example.server.web.domain.dto.*;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.domain.vo.TeacherVO;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.TeacherMapper;
import com.example.server.web.service.SysUserService;
import com.example.server.web.service.TeacherService;
import com.example.server.web.utils.MyObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SysUserService sysUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addTeacher(TeacherAddDto teacherAddDto) {
        SysUserDto sysUserDto = teacherAddDto.getSysUserDto();
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(2L);
        sysUserDto.setRoleIds(roleIds);
        sysUserService.insertUser(sysUserDto);
        //获得用户id
        SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, sysUserDto.getUsername()));
        Long userId = user.getId();
        if(userId == null){
            throw new CustomException("用户添加失败");
        }
        //复制
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAddDto,teacher);
        teacher.setUserId(userId);
        //插入
        int insert = teacherMapper.insert(teacher);
        if(insert < 1){
            throw new CustomException("添加失败");
        }
    }

    @Override
    public Map getPage(PageQueryDto pageQueryDto, TeacherParamDto teacherParamDto) {
        //查询分页数据和总记录数
        List<TeacherVO> teacherVOList=teacherMapper.getPage((pageQueryDto.getPage()-1)* pageQueryDto.getLimit(),pageQueryDto.getLimit(),teacherParamDto);
        Long total = teacherMapper.getPageTotal(teacherParamDto);
        Map<String, Object> map = new HashMap<>();
        map.put("list",teacherVOList);
        map.put("total",total);
        return map;
    }

    @Override
    public void updateTeacher(TeacherAddDto teacherAddDto) {
        //取出教师对象
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAddDto,teacher);
        //取出用户对象
        SysUserDto sysUserDto = teacherAddDto.getSysUserDto();
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(2L);
        sysUserDto.setRoleIds(roleIds);
        if (teacher.getId() == null || sysUserDto.getId() == null){
            throw new CustomException("更新失败");
        }
        //更新用户
        Integer updateUser = sysUserService.updateUser(sysUserDto,false);
        //更新教师
        int updateStudent = teacherMapper.updateById(teacher);
        if (updateStudent < 1 || updateUser < 1){
            throw new CustomException("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteTeacher(List<Long> ids) {
        for (Long id : ids) {
            Teacher teacher = this.getById(id);
            MyObjectUtils.throwExIfNull(teacher,"学生信息不存在");
            sysUserService.deleteUser(teacher.getUserId());
            //删除教师
            teacherMapper.deleteById(id);
        }
    }

    @Override
    public TeacherAddDto selectDetail(Long id) {
        TeacherAddDto teacherAddDto = new TeacherAddDto();
        Teacher teacher = teacherMapper.selectById(id);
        SysUser sysUser = sysUserService.getUserById(teacher.getUserId());
        MyObjectUtils.throwExIfNull(sysUser,"用户信息不存在");
        SysUserDto sysUserDto = new SysUserDto();
        BeanUtils.copyProperties(sysUser,sysUserDto);
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(2L);
        sysUserDto.setRoleIds(roleIds);

        BeanUtils.copyProperties(teacher,teacherAddDto);
        teacherAddDto.setSysUserDto(sysUserDto);

        return teacherAddDto;
    }

    @Override
    public String getTeacherName(Long teacherId) {
        Teacher teacher = teacherMapper.selectById(teacherId);
        if (ObjectUtils.isNull(teacher)){
            return null;
        }
        SysUser user = sysUserService.getById(teacher.getUserId());
        if (ObjectUtils.isNull(user)){
            return null;
        }
        return user.getName();
    }
}
